#include "descriptorManager.h"
#include "common.h"

int main(int argc, char** argv) {
  log_init();
  g_log_context.log_level = LOG_DEBUG;
  int ret;

  FILE* fp = fopen(argv[1], "r");assert(fp);
  uint32_t tlen;
  uint32_t rsc_num;
  uint32_t off[MAX_RSC_NUM];
  uint32_t len[MAX_RSC_NUM];
  uint32_t id = 0;
  uint32_t lid = 0;

  char* ptr = (char*)malloc(1024*1024*5);
  assert(ptr);

  char* ptr2 = (char*)malloc(1024*1024*5);
  assert(ptr2);

  while(1) {
    ret = fread(&tlen, sizeof(uint32_t), 1, fp);
    if(ret != 1 && feof(fp)) {
      NOTICE("finish");
      return 0;
    }

    ret = fread(&id, sizeof(uint32_t), 1, fp);
    assert(ret == 1);

    if(lid == 0) {lid = id;}
    else {
      if((lid+1) != id) {
	FATAL("id[%u] not sequencly sorted", id);
	assert(0);
      }
      lid++;
    }


    ret = fread(&rsc_num, sizeof(uint32_t), 1, fp);
    assert(ret == 1);

    ret = fread(off, sizeof(uint32_t), rsc_num, fp);
    assert(ret == rsc_num);

     ret = fread(len, sizeof(uint32_t), rsc_num, fp);
    assert(ret == rsc_num);

    NOTICE("id=[%u] tlen[%u], rsc[%u]", id, tlen, rsc_num);

    for(int i = 0 ; i < rsc_num ;++i) {
      NOTICE("---off[%u]  len[%u]", off[i], len[i]);
    }

    ret = fread(ptr, tlen, 1, fp);
    assert(ret == 1);

    for(int i = 0 ; i < rsc_num ; ++i) {
      memcpy(ptr2, &ptr[off[i]], len[i]);
      ptr2[len[i]] = 0;
      NOTICE("[%d][%s]", i, ptr2);
    }
  }

  return 0;
}

